package com.longtengyuyun.countDownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ZhaoYunLong
 * @date 2022/3/15
 * @description
 */
public class CountDownLatchTest {

    public static void main(String[] args) {

        Thread[] threads = new Thread[100];
        CountDownLatch countDownLatch = new CountDownLatch(threads.length);
        for (int i = 0; i< threads.length; i++) {
            threads[i] = new Thread(()->{
                int result = 0;
                for (int j=0;j<1000; j++) {
                    result +=j;
                }
                countDownLatch.countDown();
            });
        }
        for (Thread thread : threads) {
            thread.start();
        }
        try {
            System.out.println("主线程"+Thread.currentThread().getName()+"等待子线程执行完成...");
                countDownLatch.await();//阻塞当前线程，直到计数器的值为0
                System.out.println("主线程"+Thread.currentThread().getName()+"开始执行...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
